package com.ray.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.DutyBean;
import com.ray.bean.MaterialTechnologyBean;
import com.ray.exception.ServiceException;
import com.ray.service.MaterialTypeTechnologyService;
import com.ray.util.ExcelExportUtil;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;
import com.ray.util.ReturnStringUtil;

import org.springframework.web.servlet.DispatcherServlet;

@Controller
@RequestMapping("materialTypeTechnology")
public class MaterialTypeTechnologyController {
	@Autowired
	MaterialTypeTechnologyService materialTypeTechnologyService;
    
	/**
	 * 获取公司物料工艺
	 * @param status(状态  0使用  2禁用)
	 * @return
	 */
	@RequestMapping("/getCompanyMaterialTechnology")
	@ResponseBody
	public Map getCompanyMaterialTechnology(HttpServletRequest request) {
		Map map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(materialTypeTechnologyService.getCompanyMaterialTechnology(map));
	}
	
	/**
	 * 添加物料工艺
	 * @param materialType(物料类型)
	 * @param technology(工艺)
	 * @return
	 */
	@RequestMapping("/addMaterialTechnology")
	@ResponseBody
	public Map addMaterialTechnology(HttpServletRequest request) {
		Map map = RequestUtil.getRequestAllParam(request);
		materialTypeTechnologyService.addMaterialTechnology(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.ADD_SUCCESS);
	}
	
	
	/**
	 * 设置物料工艺状态
	 * @param id(id)
	 * @param status(状态)
	 * @return
	 */
	@RequestMapping("/setStatus")
	@ResponseBody
	public Map setStatus(HttpServletRequest request) {
		Map map = RequestUtil.getRequestAllParam(request);
		materialTypeTechnologyService.setStatus(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.SET_SUCCESS);
	}
	
	
	/**
	 * 修改物料工艺
	 * @param id(id)
	 * @param materialType(物料类型)
	 * @param technology(工艺)
	 * @return
	 */
	@RequestMapping("/updateMaterialTechnology")
	@ResponseBody
	public Map updateMaterialTechnology(HttpServletRequest request) {
		Map map = RequestUtil.getRequestAllParam(request);
		materialTypeTechnologyService.updateMaterialTechnology(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.UPDATE_SUCCESS);
	}
	
	
	//物料工艺数据
	@RequestMapping(value="/downlodeMaterialTechnology",method=RequestMethod.GET)
	@ResponseBody
	public void downlodeMaterialTechnology(HttpServletRequest request,HttpServletResponse response) throws ServiceException, CloneNotSupportedException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, IOException{
		Map map=RequestUtil.getRequestAllParam(request);
		map.put("start", null);
		map.put("length", null);
		DatatablesViewPageBean<MaterialTechnologyBean> datatablesViewPageBean=materialTypeTechnologyService.getCompanyMaterialTechnology(map);
		List<MaterialTechnologyBean> tableList=datatablesViewPageBean.getReturnData();
		for(MaterialTechnologyBean ma:tableList) {
			if(ma.getStatus()==0) {
				ma.setStatusStr("使用");
			}else {
				ma.setStatusStr("禁用");
			}
		}
		String [] tableHeadFields={"物料类型","工艺","状态"};
		String [] fields={"materialType","technology","statusStr"};
		String tempFolder=request.getServletContext().getRealPath("/")+"tempExcelFolder";
		File excelFile=ExcelExportUtil.createExcelFile(tableList,tempFolder,tableHeadFields,fields);
		ResponseUtil.downloadfile(response, excelFile, "物料工艺.xlsx", true);
	}
	
	/**
	 * 从订单明细拓展表中获取订单明细信息
	 * @param orderNo(系统订单编号  必填)
	 * @param lineNo(行号 非必填)
	 * @param materialFactory(仓位 非必填)
	 * @param materialType(物料类型 非必填)
	 * @return
	 */
	@RequestMapping("/getOrderDatailFromExtend")
	@ResponseBody
	public Map getOrderDatailFromExtend(HttpServletRequest request) {
		Map map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(materialTypeTechnologyService.getOrderDatailFromExtend(map));
	}
	
	
	/**
	 * 设置订单明细拓展表信息
	 * @param jsonArray(json数组字符串)
	 * @param orderNo(系统订单编号  必填)
	 * @param lineNo(行号 必填)
	 * @param materialFactory(仓位 必填)
	 * @param materialType(物料类型 必填)
	 * @param arts(物料类型 根据配置填不填)
	 * @return
	 */
	@RequestMapping("/setOrderDatailExtendDetail")
	@ResponseBody
	public Map setOrderDatailExtendDetail(HttpServletRequest request) {
		Map map = RequestUtil.getRequestAllParam(request);
		materialTypeTechnologyService.setOrderDatailExtendDetail(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.SET_SUCCESS);
	}

}
